#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,a,b) for(int i = (a);i <= (b);++i)
#define per(i,a,b) for(int i = (a);i >= (b);--i) 
#define mkp std::make_pair
typedef long long ll;
typedef unsigned long long ull;
using std::string;using std::cin;using std::cout;
inline bool cmp(int x,int y){return x < y;}
/* -fsanitize=undefined  */
const int N = 1e7+9;
const int inf = 1e9+9;
const double eps = 1e-7;
int _,n,m,u,v,x,y,cnt;
ll map[111][111],tmp,ans,a[5];


int main(){
    std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    #ifdef LOCAL //  -DLOCAL
        freopen("in.in", "r", stdin);
    #endif
    cin >> _;
    while(_--){
        ans = 0;
        cin >> n >> m;
        rep(i,1,n) rep(j,1,m) cin >> map[i][j];
        rep(i,1,(n/2) + (n%2)) rep(j,1,(m/2) + (m%2)){
            if(i == n-i+1 && j == m-j+1) continue;
            a[1] = map[i][j] , a[2] = map[n-i+1][j] , a[3] = map[i][m-j+1] , a[4] = map[n-i+1][m-j+1];
            std::sort(a+1,a+5);
            tmp = a[2];
            // cout << tmp << "\n";
            if(i == n-i+1 || j == m-j+1) rep(k,2,3) ans += std::abs(a[k]-tmp);
            else rep(k,1,4) ans += std::abs(a[k]-tmp);
        }   
        cout << ans << "\n";
    }
    return 0;
}
